package top.chukongxiang.monitorservice.javafx.fxml;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import top.chukongxiang.monitorservice.components.StreamProgressAdapter;
import top.chukongxiang.monitorservice.config.SdkServerContextHolder;
import top.chukongxiang.monitorservice.javafx.core.FxContextHolder;
import top.chukongxiang.monitorservice.javafx.fxml.components.LoadingAlert;
import top.chukongxiang.monitorservice.model.entity.ServerConfig;
import top.chukongxiang.monitorservice.tools.AlertUtil;
import top.chukongxiang.monitorservice.tools.FxBeanUtil;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/27 16:29
 */
@Slf4j
public class PcnvrConfigGui implements Initializable {

    @FXML
    public ChoiceBox<String> diskChoiceBox;

    @FXML
    public Button formatBtn;

    private static ServerConfig currentServerConfig;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<String> items = diskChoiceBox.getItems();
        if (items == null) {
            items = FXCollections.observableArrayList();
            diskChoiceBox.setItems(items);
        }
    }

    public void loadData(ServerConfig serverConfig) {
        currentServerConfig = serverConfig;
        Byte diskCount = SdkServerContextHolder.getDiskCount(serverConfig.getId());
        ObservableList<String> items = diskChoiceBox.getItems();
        items.clear();
        for (Byte i = 0; i < diskCount; i++) {
            items.add("硬盘" + i);
        }
        if (diskCount >= 1) {
            diskChoiceBox.getSelectionModel().select(0);
        }
    }

    public void formatClick(ActionEvent event) {
        Stage stage = FxContextHolder.get(ServersGui.KEY);
        int selectedIndex = diskChoiceBox.getSelectionModel().getSelectedIndex();
        if (selectedIndex == -1) {
            AlertUtil.error(stage, "请选择一个硬盘");
            return;
        }

        Optional<ButtonType> buttonType =
                AlertUtil.confirm(stage, "警告", "格式化硬盘将会清空您硬盘上的所有数据，是否要继续？", null);

        if (buttonType.isEmpty()) {
            return;
        }
        ButtonType bt = buttonType.get();
        ButtonBar.ButtonData bd = bt.getButtonData();
        if (bd.isCancelButton()) {
            return;
        }

        log.info("开始格式化硬盘：{}", selectedIndex);
        LoadingAlert loading = AlertUtil.loading(stage, "格式化中...");
        ProgressBar progressBar = loading.addProgressBar();
        Platform.runLater(() -> {
            FxBeanUtil.getHcNetSdkService().formatDisk(currentServerConfig.getId(), selectedIndex, new StreamProgressAdapter() {
                @Override
                public void start() {
                    loading.show();
                }

                @Override
                public void progress(long total, long progressSize) {
                    progressBar.setProgress(progressSize / (double) total);
                }

                @Override
                public void finish() {
                    loading.close();
                    AlertUtil.info(stage, "格式化完成");
                }
            });

        });
    }
}
